﻿using System;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using Medianamik.Core;

namespace Medianamik.UI.Controls
{
    [ToolboxData("<{0}:LanguagePicker runat=server></{0}:LanguagePicker>")]
    public class LanguagePicker : CompositeControl
    {
        public new BasePage Page
        {
            get { return base.Page as BasePage; }
        }

        public string OnClientLanguagePicking
        {
            get { return ViewState["OnClientLanguagePicking"] as string ?? ""; }
            set { ViewState["OnClientLanguagePicking"] = value; }
        }

        protected override HtmlTextWriterTag TagKey
        {
            get { return HtmlTextWriterTag.Div; }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (Page.IsPostBack)
            {
                if (Page.EditionCultureHasChanged)
                {
                    DataBind();
                    Page.FireCultureChangedEvent();
                }
            }
            else
            {
                DataBind();
            }
        }

        protected override void CreateChildControls()
        {
            var panel = new UpdatePanel {ID = ControlIDs.LanguagePickerID};
            Controls.AddAt(0, panel);
            panel.ContentTemplate = new LanguagePickerTemplate(OnClientLanguagePicking);

            if (!Page.IsPostBack)
            {
                panel.UpdateMode = UpdatePanelUpdateMode.Conditional;
            }
        }
    }

    public class LanguagePickerTemplate : ITemplate
    {
        private readonly Literal _languagePicker = new Literal();
        private readonly string _onClientLanguagePicking = "";

        public LanguagePickerTemplate(string onClientLanguagePicking)
        {
            _onClientLanguagePicking = onClientLanguagePicking;
        }

        public void InstantiateIn(Control container)
        {
            container.Controls.Add(_languagePicker);
            _languagePicker.DataBinding += languagePicker_DataBinding;
        }

        private void languagePicker_DataBinding(object sender, EventArgs e)
        {
            var page = (BasePage) _languagePicker.NamingContainer.Page;
            var cultures = page.EditionCultures.Select(c => c.CultureInfo).ToList();

            _languagePicker.Text = String.Format("<ul id=\"{0}\" class=\"Langues\">\n", ControlIDs.LanguagePickerID);

            foreach (var culture in cultures)
            {
                if (MedianamikContext.Current.EditionCulture.Equals(culture))
                    _languagePicker.Text +=
                        String.Format("<li><a class=\"Actif\" disabled=\"disabled\" title=\"{1}\">{0}</a></li>",
                                      culture.TwoLetterISOLanguageName.ToUpper(), culture.DisplayName);
                else
                {
                    if (_onClientLanguagePicking == "")
                    {
                        if (page.CurrentPage.IsEditionPage)
                            _languagePicker.Text +=
                                String.Format(
                                    "<li><a href=\"javascript:if(confirm('{0}')){{__doPostBack('{1}','{2}');}}\" title=\"{4}\">{3}</a></li>",
                                    page.SwitchLangMessage, ControlIDs.LanguagePickerID, culture.Name,
                                    culture.TwoLetterISOLanguageName.ToUpper(), culture.DisplayName);
                        else
                            _languagePicker.Text +=
                                String.Format(
                                    "<li><a href=\"javascript:__doPostBack('{0}','{1}')\" title=\"{3}\">{2}</a></li>",
                                    ControlIDs.LanguagePickerID, culture.Name,
                                    culture.TwoLetterISOLanguageName.ToUpper(), culture.DisplayName);
                    }
                    else
                    {
                        String.Format("<li><a href=\"javascript:{0};\" title=\"{2}\">{1}</a></li>",
                                      _onClientLanguagePicking, culture.TwoLetterISOLanguageName.ToUpper(),
                                      culture.DisplayName);
                    }
                }
            }

            _languagePicker.Text += "</ul>\n";
        }
    }
}